<?php
namespace app\admin\controller;
use app\admin\controller\BaseController;
use think\Request;
/**
 * 
 */
class InfoController extends BaseController
{
	protected $request,$m,$param;
	function __construct(Request $request)
	{
		parent::__construct();
		$this->request=$request;
		$this->param=$request->param();
		$this->m=model($this->request->param('m'));
	}

	public function addAction(){
		if ($this->request->isPost()) { 
			$res=$this->m->add($this->param); 
			if ($res) {  
				$this->success('添加成功');
			}else{
				$this->error($this->m->getError());
			}
		}
		$this->assign([
			'r'=>$this->m->find(0),
		]);
		return $this->fetch('add_'.$this->param['m']);
	}

	public function listAction(){ 
		$list=$this->m->order('id desc')->paginate('',false,['query'=>$this->request->get()]);
		$this->assign([
			'list'=>$list,
		]);
		if (file_exists(ROOT_PATH.'application/admin/view/info/list_'.$this->param['m'].'.html')) {
			return $this->fetch('list_'.$this->param['m']);
		}
		return $this->fetch();
	}

	public function editAction(){
		$id=$this->param['id'];
		if (!$id) {
			$this->error('参数错误');
		}
		if ($this->request->isPost()) {
			$res=$this->m->edit($this->param);
			if ($res) {
				$this->success('编辑成功');
			}else{
				$this->error($this->m->getError());
			}
		}
		$this->assign([
			'r'=>$this->m->find($id),
		]);
		return $this->fetch('add_'.$this->param['m']);
	}

	public function delAction(){
		$res=$this->m->del($this->request->param('id'));
		if ($res) {
			$this->success('删除成功');
		}else{
			$this->error($this->m->getError());
		}
	}
}